package com.ouyang.controller;

import com.ouyang.Service.LoginService;
import com.ouyang.dto.UserDto;
import com.ouyang.form.UserForm;
import com.ouyang.poji.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.management.monitor.Monitor;
import javax.servlet.http.HttpSession;


@Controller
@RequestMapping("/account")
public class AccountController {

    @Autowired
    private LoginService loginService;

    @RequestMapping("/login")
    public String login ( Model model, UserForm user, HttpSession session ) {
        UserDto userDto = loginService.login(user);
        Integer code = userDto.getCode();
        User user1 = userDto.getUser();
        if (code == 1) {
            session.setAttribute("user", user1);
            return "index";
        } else if (code == -1) {
            model.addAttribute("error", "用户名或者密码错误");
            return "login";
        } else {
            model.addAttribute("error", "用户名不存在");
            return "login";
        }

    }

    @RequestMapping("/loginout")
    public String loginout ( HttpSession session ) {
        session.removeAttribute("user");
        return "error";

    }

}
